package com.hafiery.sdk.bluetooth.test;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import com.hafiery.sdk.bluetooth.IBlueToothAction;
import com.hafiery.sdk.bluetooth.api.BlueToothSDK;
import com.hafiery.sdk.bluetooth.api.IBlueToothApi;
import com.hafiery.sdk.bluetooth.test.databinding.ActivityConfigBinding;
import com.hafiery.sdk.bluetooth.test.message.GattDisconnectMessage;
import com.hafiery.sdk.bluetooth.transport.callback.Callback;
import com.hafiery.sdk.bluetooth.transport.constant.ErrorCode;
import com.hafiery.sdk.bluetooth.transport.task.TaskManager;
import com.hafiery.sdk.bluetooth.util.LogUtil;
import com.tencent.bugly.crashreport.CrashReport;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class ConfigActivity extends AppCompatActivity implements View.OnClickListener {
    private ActivityConfigBinding binding;
    private IBlueToothApi api;
    private IBlueToothAction blueToothClient;
    private ExecutorService executor = Executors.newFixedThreadPool(1);
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
        api=BlueToothSDK.instance().getBlueToothApi();
        blueToothClient = BlueToothSDK.instance().getBlueToothClient();
        binding=DataBindingUtil.setContentView(this,R.layout.activity_config);
        binding.toolbar.setTitle(getString(R.string.config_title));
        setSupportActionBar(binding.toolbar);

        binding.settingsDeviceTime.setOnClickListener(this);
        binding.settingsDeviceVersion.setOnClickListener(this);
        binding.settingsDeviceBind.setOnClickListener(this);
        binding.settingsDeviceBleName.setOnClickListener(this);
        binding.settingsDeviceWifiCurrent.setOnClickListener(this);
        binding.settingsDeviceRecordMode.setOnClickListener(this);
        binding.settingsDeviceRecordStart.setOnClickListener(this);
        binding.settingsDeviceRecordEnd.setOnClickListener(this);
        binding.settingsDeviceRecordPause.setOnClickListener(this);
        binding.settingsDeviceRecordResume.setOnClickListener(this);
        binding.settingsDeviceRecordList.setOnClickListener(this);
        binding.settingsDeviceRecordReFactory.setOnClickListener(this);
        binding.settingsDeviceRecordFormatRom.setOnClickListener(this);
        binding.settingsDeviceWifiRssi.setOnClickListener(this);
        binding.settingsDeviceLivePause.setOnClickListener(this);
        binding.settingsDeviceLiveResume.setOnClickListener(this);


        binding.settingsDeviceWifiStatus.tailSwitch.setOnCheckedChangeListener((buttonView, isChecked) -> {
            api.openOrCloseWifi(isChecked, new Callback<String>() {
                @Override
                public void onSuccess(String data) {
                    binding.settingsDeviceWifiStatus.tailSwitch.setChecked(isChecked);
                    if(!isChecked){
                        binding.settingsDeviceWifiCurrent.tailText.setText("");
                    }
//                    api.getCurrentWifi(new Callback<String>() {
//                        @Override
//                        public void onSuccess(String data) {
//                            binding.settingsDeviceWifiCurrent.tailText.setText(TextUtils.isEmpty(data)?"":data);
//                        }
//
//                        @Override
//                        public void onFailure(ErrorCode errorCode) {
//
//                        }
//                    });
                }
                @Override
                public void onFailure(ErrorCode errorCode) {

                }
            });
        });
        binding.settingsDeviceRecordAuto.tailSwitch.setOnCheckedChangeListener((buttonView, isChecked) -> {
            api.setAutoRecordStatus(isChecked, new Callback<String>() {
                @Override
                public void onSuccess(String data) {

                }

                @Override
                public void onFailure(ErrorCode errorCode) {
                    LogUtil.log("设置自动录音失败:"+errorCode.getMessage());
                }
            });
        });
        binding.settingsDeviceRecordOriginStatus.tailSwitch.setOnCheckedChangeListener((buttonView, isChecked) -> {
            api.setOriginRecordStatus(isChecked, new Callback<String>() {
                @Override
                public void onSuccess(String data) {

                }

                @Override
                public void onFailure(ErrorCode errorCode) {
                    LogUtil.log("设置保留原始录音失败:"+errorCode.getMessage());
                }
            });
        });
        binding.settingsDeviceRecordLight.tailSwitch.setOnCheckedChangeListener((buttonView, isChecked) -> {
            api.setRecordLightStatus(isChecked, new Callback<String>() {
                @Override
                public void onSuccess(String data) {

                }

                @Override
                public void onFailure(ErrorCode errorCode) {
                    LogUtil.log("设置录音指示灯失败:"+errorCode.getMessage());
                }
            });
        });
        binding.settingsDeviceLiveTransform.tailSwitch.setOnCheckedChangeListener((buttonView, isChecked) -> {
            api.openOrCloseLiveTransform(isChecked, new Callback<String>() {
                @Override
                public void onSuccess(String data) {

                }

                @Override
                public void onFailure(ErrorCode errorCode) {
                    LogUtil.log("实时传输开关开启失败:"+errorCode.getMessage());
                }
            });
        });

        initData();
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.toolbar_config,menu);
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case R.id.refresh:
                initData();
                break;
            case R.id.disConnect:
                showDisconnectDialog();
                break;
        }
        return true;
    }



    private void initData(){
        api.getDeviceType(new Callback<String>() {
            @Override
            public void onSuccess(String data) {
                binding.settingsDeviceType.tailText.setText(data);
            }
            @Override
            public void onFailure(ErrorCode errorCode) {
                LogUtil.log("获取设备类型失败:"+errorCode.getMessage());
            }
        });
        api.getDeviceID(new Callback<String>() {
            @Override
            public void onSuccess(String data) {
                binding.settingsDeviceId.tailText.setText(data);
            }
            @Override
            public void onFailure(ErrorCode errorCode) {
                LogUtil.log("获取设备ID失败:"+errorCode.getMessage());
            }
        });
        api.getDeviceTime(new Callback<Integer>() {
            @Override
            public void onSuccess(Integer data) {
                SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String dateString=simpleDateFormat.format(new Date(data*1000l));
                binding.settingsDeviceTime.tailText.setText(dateString);
            }
            @Override
            public void onFailure(ErrorCode errorCode) {
                LogUtil.log("获取设备时间失败:"+errorCode.getMessage());
            }
        });
        api.getDeviceVersion(new Callback<String>() {
            @Override
            public void onSuccess(String data) {
                binding.settingsDeviceVersion.tailText.setText(data);
            }
            @Override
            public void onFailure(ErrorCode errorCode) {
                LogUtil.log("获取设备版本失败:"+errorCode.getMessage());
            }
        });
        api.getDeviceMAC(new Callback<String>() {
            @Override
            public void onSuccess(String data) {
                String mac0=data.substring(0,12);
                String mac1 =data.substring(12);
                String result=(mac0+":"+mac1).toUpperCase();
                binding.settingsDeviceMac.tailText.setText(result);
            }
            @Override
            public void onFailure(ErrorCode errorCode) {
                LogUtil.log("获取设备MAC失败:"+errorCode.getMessage());
            }
        });

        api.getDevicePower(new Callback<Integer>() {
            @Override
            public void onSuccess(Integer data) {
                binding.settingsDevicePower.tailText.setText(data+"%");
            }
            @Override
            public void onFailure(ErrorCode errorCode) {
                LogUtil.log("获取电量失败:"+errorCode.getMessage());
            }
        });

        api.getDeviceRom(new Callback<Integer>() {
            @Override
            public void onSuccess(Integer data) {
                binding.settingsDeviceRom.tailText.setText(String.format("共%sGB",String.format("%.1f",data*1.0f/1024/1024)));
            }
            @Override
            public void onFailure(ErrorCode errorCode) {
                LogUtil.log("获取设备总存储失败:"+errorCode.getMessage());
            }
        });
        api.getDeviceRomUsed(new Callback<Integer>() {
            @Override
            public void onSuccess(Integer data) {
                String value=binding.settingsDeviceRom.tailText.getText().toString();
                binding.settingsDeviceRom.tailText.setText(value+" "+String.format("已用%sGB",String.format("%.1f",data*1.0f/1024/1024)));
            }
            @Override
            public void onFailure(ErrorCode errorCode) {
                LogUtil.log("获取设备存储已使用失败:"+errorCode.getMessage());
            }
        });
        api.getDeviceRomUnUsed(new Callback<Integer>() {
            @Override
            public void onSuccess(Integer data) {
                String value=binding.settingsDeviceRom.tailText.getText().toString();
                binding.settingsDeviceRom.tailText.setText(value+" "+String.format("剩余%sGB",String.format("%.1f",data*1.0f/1024/1024)));
            }
            @Override
            public void onFailure(ErrorCode errorCode) {
                LogUtil.log("获取设备存储未使用失败:"+errorCode.getMessage());
            }
        });
        api.getDeviceBindInfo(new Callback<String>() {
            @Override
            public void onSuccess(String data) {
                binding.settingsDeviceBind.tailText.setText(data);
            }
            @Override
            public void onFailure(ErrorCode errorCode) {
                LogUtil.log("获取设备绑定信息失败:"+errorCode.getMessage());
            }
        });
        api.getBleName(new Callback<String>() {
            @Override
            public void onSuccess(String data) {
                binding.settingsDeviceBleName.tailText.setText(data);
            }
            @Override
            public void onFailure(ErrorCode errorCode) {
                LogUtil.log("获取设备蓝牙名称失败:"+errorCode.getMessage());
            }
        });
        api.getWifiStatus(new Callback<Integer>() {
            @Override
            public void onSuccess(Integer data) {
                binding.settingsDeviceWifiStatus.tailSwitch.setChecked(data==1);
            }

            @Override
            public void onFailure(ErrorCode errorCode) {
                LogUtil.log("获取wifi状态失败:"+errorCode.getMessage());
            }
        });
        api.getCurrentWifi(new Callback<String>() {
            @Override
            public void onSuccess(String data) {
                binding.settingsDeviceWifiCurrent.tailText.setText(TextUtils.isEmpty(data)?"":data);
            }

            @Override
            public void onFailure(ErrorCode errorCode) {
                LogUtil.log("获取当前wifi失败:"+errorCode.getMessage());
            }
        });
        api.getAutoRecordStatus(new Callback<Integer>() {
            @Override
            public void onSuccess(Integer data) {
                binding.settingsDeviceRecordAuto.tailSwitch.setChecked(data==1);
            }

            @Override
            public void onFailure(ErrorCode errorCode) {
                LogUtil.log("获取自动录音状态失败:"+errorCode.getMessage());
            }
        });

        api.getOriginRecordStatus(new Callback<Integer>() {
            @Override
            public void onSuccess(Integer data) {
                binding.settingsDeviceRecordOriginStatus.tailSwitch.setChecked(data==1);
            }

            @Override
            public void onFailure(ErrorCode errorCode) {
                LogUtil.log("获取原始录音状态信息失败:"+errorCode.getMessage());
            }
        });
        api.getRecordModel(new Callback<Integer>() {
            @Override
            public void onSuccess(Integer data) {
                int index=data-1;
                binding.settingsDeviceRecordMode.tailText.setTag(index);
                binding.settingsDeviceRecordMode.tailText.setText(getResources().getStringArray(R.array.record_mode)[index]);
            }

            @Override
            public void onFailure(ErrorCode errorCode) {
                LogUtil.log("获取录音模式失败:"+errorCode.getMessage());
            }
        });
        api.getRecordFormat(new Callback<Integer>() {
            @Override
            public void onSuccess(Integer data) {
                binding.settingsDeviceRecordFormat.tailText.setText(getResources().getStringArray(R.array.audio_format)[data]);
            }

            @Override
            public void onFailure(ErrorCode errorCode) {
                LogUtil.log("获取录音格式失败:"+errorCode.getMessage());
            }
        });
        api.getRecordLightStatus(new Callback<Integer>() {
            @Override
            public void onSuccess(Integer data) {
                binding.settingsDeviceRecordLight.tailSwitch.setChecked(data==1);
            }

            @Override
            public void onFailure(ErrorCode errorCode) {
                LogUtil.log("获取录音指示灯失败:"+errorCode.getMessage());
            }
        });
        api.getCurrentWifiRssi(new Callback<Integer>() {
            @Override
            public void onSuccess(Integer data) {
                binding.settingsDeviceWifiRssi.tailText.setText("-"+data);
            }

            @Override
            public void onFailure(ErrorCode errorCode) {
                LogUtil.log("获取当前wifi信息失败:"+errorCode.getMessage());
            }
        });
        recordRefresh();
    }

    private void recordRefresh(){
        api.getRecordStatus(new Callback<Integer>() {
            @Override
            public void onSuccess(Integer data) {
                binding.settingsDeviceRecordStatus.tailText.setText(data==0?"未录音":"正在录音");
            }

            @Override
            public void onFailure(ErrorCode errorCode) {
                LogUtil.log("获取录音状态失败:"+errorCode.getMessage());
            }
        });
        api.getCurrentRecordFileName(new Callback<String>() {
            @Override
            public void onSuccess(String data) {
                binding.settingsDeviceRecordCurrentName.tailText.setText(data);
            }

            @Override
            public void onFailure(ErrorCode errorCode) {
                LogUtil.log("获取当前录音文件名失败:"+errorCode.getMessage());
            }
        });
    }


    private void showConfirmDialog(String title, String message, DialogInterface.OnClickListener confirmListener){
        new AlertDialog.Builder(ConfigActivity.this)
                .setTitle(title)
                .setMessage(message)
                .setNegativeButton("取消", (dialog, which) -> {
                })
                .setPositiveButton("确定",confirmListener)
                .show();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.settings_device_time:
                showConfirmDialog("提示", "是否同步时间?", (dialog, which) -> {
                    api.setDeviceTime(System.currentTimeMillis(), new Callback<String>() {
                        @Override
                        public void onSuccess(String data) {
                            LogUtil.log("同步时间成功");
                            api.getDeviceTime(new Callback<Integer>() {
                                @Override
                                public void onSuccess(Integer data) {
                                    SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                                    String dateString=simpleDateFormat.format(new Date(data*1000l));
                                    binding.settingsDeviceTime.tailText.setText(dateString);
                                }
                                @Override
                                public void onFailure(ErrorCode errorCode) {
                                    LogUtil.log("获取设备时间失败:"+errorCode.getMessage());
                                }
                            });

                        }
                        @Override
                        public void onFailure(ErrorCode errorCode) {
                            LogUtil.log("同步时间失败");
                        }
                    });
                });
                break;
            case R.id.settings_device_version:
                showConfirmDialog("提示", "是否进行升级?", (dialog, which) -> {
                    api.upgradeDeviceVersion(new Callback<String>() {
                        @Override
                        public void onSuccess(String data) {
                            LogUtil.log("发送升级成功");
                        }
                        @Override
                        public void onFailure(ErrorCode errorCode) {
                            LogUtil.log("同步时间失败");
                        }
                    });
                });
                break;
            case R.id.settings_device_bind:
                String bindInfo =binding.settingsDeviceBind.tailText.getText().toString();
                if(TextUtils.isEmpty(bindInfo)){
                    EditText editText = new EditText(ConfigActivity.this);
                    editText.setPadding(50,20,50,20);
                    editText.setText(bindInfo);
                    new AlertDialog.Builder(ConfigActivity.this)
                            .setTitle("提示")
                            .setMessage("请输入绑定信息")
                            .setNegativeButton("取消", (dialog, which) -> {
                            })
                            .setPositiveButton("确定", (dialog, which) -> {
                                String value =editText.getText().toString();
                                if(!TextUtils.isEmpty(value)){
                                    api.bindDevice(value, new Callback<String>() {
                                        @Override
                                        public void onSuccess(String data) {
                                            binding.settingsDeviceBind.tailText.setText(value);
                                        }

                                        @Override
                                        public void onFailure(ErrorCode errorCode) {

                                        }
                                    });
                                }
                            })
                            .setView(editText)
                            .show();
                }else{
                    showConfirmDialog("提示", "是否解绑该设备?", (dialog, which) -> {
                        api.unBindDevice(new Callback<String>() {
                            @Override
                            public void onSuccess(String data) {
                                binding.settingsDeviceBind.tailText.setText("");
                            }
                            @Override
                            public void onFailure(ErrorCode errorCode) {
                                LogUtil.log("同步时间失败");
                            }
                        });
                    });
                }
                break;
            case R.id.settings_device_ble_name:
                String bleName =binding.settingsDeviceBleName.tailText.getText().toString();
                EditText editText = new EditText(ConfigActivity.this);
                editText.setPadding(50,20,50,20);
                editText.setText(bleName);
                new AlertDialog.Builder(ConfigActivity.this)
                        .setTitle("提示")
                        .setMessage("请输入绑定信息")
                        .setNegativeButton("取消", (dialog, which) -> {
                        })
                        .setPositiveButton("确定", (dialog, which) -> {
                            String value =editText.getText().toString();
                            if(!TextUtils.isEmpty(value)){
                                api.bindBleName(value, new Callback<String>() {
                                    @Override
                                    public void onSuccess(String data) {
                                        binding.settingsDeviceBleName.tailText.setText(value);
                                    }

                                    @Override
                                    public void onFailure(ErrorCode errorCode) {

                                    }
                                });
                            }
                        })
                        .setView(editText)
                        .show();
                break;
            case R.id.settings_device_wifi_rssi:
                new AlertDialog.Builder(ConfigActivity.this)
                        .setTitle("提示")
                        .setMessage("是否获取wifi强度?")
                        .setNegativeButton("取消", (dialog, which) -> {
                        })
                        .setPositiveButton("确定", (dialog, which) -> {
                            api.getCurrentWifiRssi(new Callback<Integer>() {
                                @Override
                                public void onSuccess(Integer data) {
                                    binding.settingsDeviceWifiRssi.tailText.setText("-"+data);
                                }

                                @Override
                                public void onFailure(ErrorCode errorCode) {

                                }
                            });
                        })
                        .show();
                break;
            case R.id.settings_device_wifi_current:
                String currentWifi =binding.settingsDeviceWifiCurrent.tailText.getText().toString();
                if(TextUtils.isEmpty(currentWifi)){
                    LinearLayout linearLayout=new LinearLayout(ConfigActivity.this);
                    linearLayout.setOrientation(LinearLayout.VERTICAL);
                    EditText ssidEdit = new EditText(ConfigActivity.this);
                    ssidEdit.setPadding(50,20,50,20);
                    ssidEdit.setHint("请输入ssid");
                    EditText pwdEdit = new EditText(ConfigActivity.this);
                    pwdEdit.setPadding(50,20,50,20);
                    pwdEdit.setHint("请输入password");
                    linearLayout.addView(ssidEdit);
                    linearLayout.addView(pwdEdit);
                    new AlertDialog.Builder(ConfigActivity.this)
                            .setTitle("提示")
                            .setMessage("请输入wifi信息")
                            .setNegativeButton("取消", (dialog, which) -> {
                            })
                            .setPositiveButton("确定", (dialog, which) -> {
                                String ssid =ssidEdit.getText().toString();
                                String password =pwdEdit.getText().toString();
                                if(!TextUtils.isEmpty(ssid)){
                                    api.connectWifi(ssid,password, new Callback<String>() {
                                        @Override
                                        public void onSuccess(String data) {
                                            binding.settingsDeviceWifiCurrent.tailText.setText(ssid);
                                            binding.settingsDeviceWifiStatus.tailSwitch.setChecked(true);
                                            api.getCurrentWifiRssi(new Callback<Integer>() {
                                                @Override
                                                public void onSuccess(Integer data) {
                                                    binding.settingsDeviceWifiRssi.tailText.setText("-"+data);
                                                }

                                                @Override
                                                public void onFailure(ErrorCode errorCode) {

                                                }
                                            });
//                                            api.getWifiStatus(new Callback<Integer>() {
//                                                @Override
//                                                public void onSuccess(Integer data) {
//                                                    binding.settingsDeviceWifiStatus.tailSwitch.setChecked(data==1);
//                                                }
//
//                                                @Override
//                                                public void onFailure(ErrorCode errorCode) {
//
//                                                }
//                                            });
//                                            api.getCurrentWifi(new Callback<String>() {
//                                                @Override
//                                                public void onSuccess(String data) {
//                                                    binding.settingsDeviceWifiCurrent.tailText.setText(TextUtils.isEmpty(data)?"":data);
//                                                }
//
//                                                @Override
//                                                public void onFailure(ErrorCode errorCode) {
//
//                                                }
//                                            });
                                        }

                                        @Override
                                        public void onFailure(ErrorCode errorCode) {
                                            Toast.makeText(ConfigActivity.this,errorCode.getMessage(),Toast.LENGTH_LONG).show();
                                        }
                                    });
                                }else{
                                    Toast.makeText(ConfigActivity.this,"ssid不能为空",Toast.LENGTH_LONG).show();
                                }
                            })
                            .setView(linearLayout)
                            .show();
                }else {
                    showConfirmDialog("提示", "是否断开当前wifi?", (dialog, which) -> {
                        api.disConnectWifi(currentWifi,new Callback<String>() {
                            @Override
                            public void onSuccess(String data) {
                                binding.settingsDeviceWifiCurrent.tailText.setText("");
//                                api.getCurrentWifi(new Callback<String>() {
//                                    @Override
//                                    public void onSuccess(String data) {
//                                        binding.settingsDeviceWifiCurrent.tailText.setText(TextUtils.isEmpty(data)?"":data);
//                                    }
//
//                                    @Override
//                                    public void onFailure(ErrorCode errorCode) {
//
//                                    }
//                                });
                            }
                            @Override
                            public void onFailure(ErrorCode errorCode) {
                                LogUtil.log("断开wifi失败");
                            }
                        });
                    });
                }
                break;
            case R.id.settings_device_record_mode:
                Object o=binding.settingsDeviceRecordMode.tailText.getTag();
                Integer i=0;
                if(o!=null){
                    i=(Integer) o;
                }
                new AlertDialog.Builder(ConfigActivity.this)
                        .setTitle("提示")
                        .setSingleChoiceItems(getResources().getStringArray(R.array.record_mode), i, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                LogUtil.log("当前选中:"+which);
                                dialog.dismiss();
                                api.setRecordModel(which+1, new Callback<String>() {
                                    @Override
                                    public void onSuccess(String data) {
                                        binding.settingsDeviceRecordMode.tailText.setTag(which);
                                        binding.settingsDeviceRecordMode.tailText.setText(getResources().getStringArray(R.array.record_mode)[which]);
                                    }

                                    @Override
                                    public void onFailure(ErrorCode errorCode) {

                                    }
                                });
                            }
                        }).show();
                break;
            case R.id.settings_device_record_start:
                api.startRecord(new Callback<String>() {
                    @Override
                    public void onSuccess(String data) {
                        Toast.makeText(ConfigActivity.this,"开始录音",Toast.LENGTH_SHORT).show();
                        recordRefresh();
                    }

                    @Override
                    public void onFailure(ErrorCode errorCode) {
                        LogUtil.log("开始录音失败:"+errorCode.getMessage());
                    }
                });
                break;
            case R.id.settings_device_record_end:
                api.stopRecord(new Callback<String>() {
                    @Override
                    public void onSuccess(String data) {
                        Toast.makeText(ConfigActivity.this,"结束录音",Toast.LENGTH_SHORT).show();
                        recordRefresh();
                    }

                    @Override
                    public void onFailure(ErrorCode errorCode) {
                        LogUtil.log("结束录音失败:"+errorCode.getMessage());
                    }
                });
                break;
            case R.id.settings_device_record_pause:
                api.pauseRecord(new Callback<String>() {
                    @Override
                    public void onSuccess(String data) {
                        Toast.makeText(ConfigActivity.this,"暂停录音",Toast.LENGTH_SHORT).show();
                        recordRefresh();
                    }

                    @Override
                    public void onFailure(ErrorCode errorCode) {
                        LogUtil.log("暂停录音失败:"+errorCode.getMessage());
                    }
                });
                break;
            case R.id.settings_device_record_resume:
                api.resumeRecord(new Callback<String>() {
                    @Override
                    public void onSuccess(String data) {
                        Toast.makeText(ConfigActivity.this,"恢复录音",Toast.LENGTH_SHORT).show();
                        recordRefresh();
                    }

                    @Override
                    public void onFailure(ErrorCode errorCode) {
                        LogUtil.log("开始录音失败:"+errorCode.getMessage());
                    }
                });
                break;
            case R.id.settings_device_record_list:
                Intent intent=new Intent(ConfigActivity.this,FileListActivity.class);
                startActivity(intent);
                break;
            case R.id.settings_device_record_reFactory:
                api.reFactory(new Callback<String>() {
                    @Override
                    public void onSuccess(String data) {
                        Toast.makeText(ConfigActivity.this,"恢复出厂设置成功",Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onFailure(ErrorCode errorCode) {

                    }
                });
                break;
            case R.id.settings_device_record_format_rom:
                api.formatRom(new Callback<String>() {
                    @Override
                    public void onSuccess(String data) {
                        Toast.makeText(ConfigActivity.this,"格式化成功",Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onFailure(ErrorCode errorCode) {

                    }
                });
                break;
            case R.id.settings_device_live_pause:
                api.pauseLiveTransform(new Callback<String>() {
                    @Override
                    public void onSuccess(String data) {
                        Toast.makeText(ConfigActivity.this,"暂停实时传输",Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onFailure(ErrorCode errorCode) {
                        LogUtil.log("暂停实时传输失败:"+errorCode.getMessage());
                    }
                });
                break;
            case R.id.settings_device_live_resume:
                api.resumeLiveTransform(new Callback<String>() {
                    @Override
                    public void onSuccess(String data) {
                        Toast.makeText(ConfigActivity.this,"继续实时传输",Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onFailure(ErrorCode errorCode) {
                        LogUtil.log("继续实时传输失败:"+errorCode.getMessage());
                    }
                });
                break;
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onGetMessage(GattDisconnectMessage message) {
        new AlertDialog.Builder(ConfigActivity.this).
                setTitle("提示")
                .setMessage("工牌连接已断开,是否重连?")
                .setNegativeButton("取消", (dialog, which) -> {

                })
                .setPositiveButton("确定", (dialog, which) -> {
                   // blueToothClient.reconnect();
                }).show();
    }

    @Override
    public void onBackPressed() {
        new AlertDialog.Builder(ConfigActivity.this).
                setTitle("提示")
                .setMessage("是否主动断开工牌?")
                .setNegativeButton("取消", (dialog, which) -> {

                })
                .setPositiveButton("确定", (dialog, which) -> {
                    blueToothClient.disconnect();
                }).show();
    }

    @Override
    protected void onDestroy() {
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }

    private void showDisconnectDialog(){
        if(blueToothClient.isConnected()){
            new AlertDialog.Builder(ConfigActivity.this).
                    setTitle("提示")
                    .setMessage("是否断开工牌?")
                    .setNegativeButton("取消", (dialog, which) -> {

                    })
                    .setPositiveButton("确定", (dialog, which) -> {
                        blueToothClient.disconnect();
                    }).show();
        }

    }
}
